﻿using UnityEngine;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using System;

public static partial class Extensions
{
    /// <summary>
    /// UI物体异步弹出显示
    /// </summary>
    public static async UniTask PopupShowAsync(this RectTransform rect, float duration = 0.25f, Ease ease = Ease.OutBack)
    {
        rect.gameObject.SetActive(true);
        rect.DOScale(1, duration).ChangeStartValue(Vector3.one * 0.5f).SetEase(ease);
        var cg = rect.GetOrCreateComponent<CanvasGroup>();
        cg.blocksRaycasts = true;
        cg.DOKill();
        cg.DOFade(1, duration).ChangeStartValue(0f);
        await Tools.Delay(duration);
    }

    /// <summary>
    /// UI物体异步弹出隐藏
    /// </summary>
    public static async UniTask PopupHideAsync(this RectTransform rect, float duration = 0.25f, Ease ease = Ease.InBack)
    {
        rect.DOKill();
        rect.DOScale(0.5f, duration).ChangeStartValue(Vector3.one).SetEase(ease);
        var cg = rect.GetOrCreateComponent<CanvasGroup>();
        cg.blocksRaycasts = false;
        cg.DOKill();
        cg.DOFade(0, duration).ChangeStartValue(1f);
        await Tools.Delay(duration);
        rect.gameObject.SetActive(false);
    }

    /// <summary>
    /// UI物体异步淡入显示
    /// </summary>
    public static async UniTask FadeShowAsync(this RectTransform rect, float duration = 0.25f)
    {
        rect.gameObject.SetActive(true);
        var cg = rect.GetOrCreateComponent<CanvasGroup>();
        cg.blocksRaycasts = true;
        cg.DOKill();
        cg.DOFade(1, duration).ChangeStartValue(0f);
        await Tools.Delay(duration);
    }

    /// <summary>
    /// UI物体异步淡出隐藏
    /// </summary>
    public static async UniTask FadeHideAsync(this RectTransform rect, float duration = 0.25f)
    {
        var cg = rect.GetOrCreateComponent<CanvasGroup>();
        cg.blocksRaycasts = false;
        cg.DOKill();
        cg.DOFade(0, duration).ChangeStartValue(1f);
        await Tools.Delay(duration);
        rect.gameObject.SetActive(false);
    }
}
